﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;
using System.Collections.Generic;
using Yipusoft.SoldierArchives.BLL;
using Yipusoft.Account.Model;
using Yipusoft.Account.BLL;

public partial class Police_SearchTransfer : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            rlDR.DataSource=BindTransferType("SBDD10");
            rlDR.DataTextField = "Text";
            rlDR.DataValueField = "Value";
            rlDR.DataBind();            
            rlDR.SelectedIndex = 0;

            rlDC.DataSource = BindTransferType("SBDD20");
            rlDC.DataTextField = "Text";
            rlDC.DataValueField = "Value";
            rlDC.DataBind();
            rlDC.SelectedIndex = 0;

            StartDate.Text = DateTime.Today.ToString("yyyy-01-01");
            EndDate.Text = DateTime.Today.ToString("yyyy-MM-dd");

            ddlDCDepartmentList.DataSource = GetDepartmentList();
            ddlDCDepartmentList.DataTextField = "Text";
            ddlDCDepartmentList.DataValueField = "Value";
            ddlDCDepartmentList.DataBind();

            ddlDRDepartmentList.DataSource = GetDepartmentList();
            ddlDRDepartmentList.DataTextField = "Text";
            ddlDRDepartmentList.DataValueField = "Value";
            ddlDRDepartmentList.DataBind();
            SetPanel();

        }
    }

    private List<ListItem> GetDepartmentList()
    {
        List<ListItem> departmentList = new List<ListItem>();
        string departmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        List<DepartmentInfo> departments = new List<DepartmentInfo>();
        if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "400101"))
        {
            departments = Department.GetList("1>0");
            departmentList.Add(new ListItem("--单位--", ""));
        }
        else
            if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "400101"))
            {
                departments = Department.GetList("Left(DepartmentID,4)='" + departmentID.Substring(0, 4) + "'");
                departmentList.Add(new ListItem("--单位--", ""));
            }
            else
            {
                departments = Department.GetList("DepartmentID='" + departmentID + "'");
                departmentList.Add(new ListItem("--单位--", ""));
            }
        foreach (DepartmentInfo departmentInfo in departments)
            departmentList.Add(new ListItem(departmentInfo.FullName, departmentInfo.DepartmentID));
        return departmentList;
    }

    private List<ListItem> BindTransferType(string parentID)
    {
        List<ListItem> liList = new List<ListItem>();

        liList.Add(new ListItem("系统内部调动", "SBDD30"));

        foreach (CodeItemInfo codeItemInfo in CodeItem.GetList("parentID='" + parentID + "' order by seqID"))
        {
            liList.Add(new ListItem(codeItemInfo.ItemName, codeItemInfo.ItemID));
        }
        return liList;
    }

    protected void gvTransfer_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        string TID = gvTransfer.DataKeys[e.NewSelectedIndex].Values["TransferID"].ToString();
        string ID = gvTransfer.DataKeys[e.NewSelectedIndex].Values["SoldierID"].ToString();
        Response.Redirect("TransfeSoldier.aspx?ID=" + ID + "&TID=" + TID);
    }
    protected void gvTransfer_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Soldier soldier = new Soldier();
            e.Row.Cells[1].Text = soldier.GetSoldierInfoByID(gvTransfer.DataKeys[e.Row.RowIndex].Values["SoldierID"].ToString()).SoldierName;
            Label ID1 = (Label)e.Row.FindControl("ID");
            if (ID1 != null)
            {
                ID1.Text = ((int)ViewState["PageIndex"] * 20 + e.Row.RowIndex + 1).ToString();
            }
            
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
    
    }


    protected void btnDR_Click(object sender, EventArgs e)
    {
        string departmentID = GetDepartmentID();

        StrSql.Value = "FromJingZhong='" + rlDR.SelectedValue + "' and TransferDate between '" + StartDate.Text + "' and '" + EndDate.Text + "' and ToUnitID='" + departmentID + "'";
        BindTransfer(0);
        gvTransfer.Columns[2].Visible = true;
        gvTransfer.Columns[3].Visible = false;
    }

    private void BindTransfer(int pageIndex)
    {
        int pageSize = 20;
        lblInfo.Text = "";
        int totalCount = TransferSoldier.GetTransfersCountByWhere(StrSql.Value);
        panPage.Visible = totalCount > 0;
        ViewState["PageCount"] = Convert.ToInt32(Math.Ceiling((double)totalCount / pageSize));
        if ((int)ViewState["PageCount"] < pageIndex)
            pageIndex = pageIndex - 1;
        ViewState["PageIndex"] = pageIndex;
        gvTransfer.DataSource = TransferSoldier.GetTransfersByWhere(StrSql.Value, 20, pageIndex);
        gvTransfer.DataBind();
        lbGridInfo.Text = "共有<b>" + totalCount.ToString() + "</b>条记录 第<b>" + ((int)ViewState["PageIndex"] + 1).ToString() + "</b>页 共<b>" + ViewState["PageCount"].ToString() + "</b>页 每页<b>" + pageSize.ToString() + "</b>条";
        this.btnFirst.Enabled = (int)ViewState["PageIndex"] > 0;
        this.btnPrev.Enabled = (int)ViewState["PageIndex"] > 0;
        this.btnNext.Enabled = (int)ViewState["PageIndex"] < (int)ViewState["PageCount"] - 1;
        this.btnLast.Enabled = (int)ViewState["PageIndex"] < (int)ViewState["PageCount"] - 1;
        BindJumpList();
    }

    private string GetDepartmentID()
    {
        string departmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "400101"))
            departmentID = departmentID.Substring(0, 2) + "00000";
        else
            if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "400102"))
                departmentID = departmentID.Substring(0, 4) + "000";
        return departmentID;
    }

    protected void btnDC_Click(object sender, EventArgs e)
    {
        string departmentID = GetDepartmentID();
        StrSql.Value = "ToJingZhong='" + rlDC.SelectedValue 
            + "' and TransferDate between '" + StartDate.Text + "' and '" + EndDate.Text 
            + "' and FromUnitID='"+departmentID+"'";
        BindTransfer(0);
        gvTransfer.Columns[2].Visible = false;
        gvTransfer.Columns[3].Visible = true;
    }

    protected void btnEx_Click(object sender, EventArgs e)
    {
        if (ddlDCDepartmentList.SelectedValue == "" && ddlDRDepartmentList.SelectedValue == "")
            lblInfo.Text = "调出单位和调入单位不能同时为空！";
        else
        {
            StrSql.Value = "TransferDate between '" + StartDate.Text + "' and '" + EndDate.Text  + "' ";
            if (ddlDRDepartmentList.SelectedValue != "")            
                StrSql.Value += " and ToUnitID='" + ddlDRDepartmentList.SelectedValue+"'";
            if (ddlDCDepartmentList.SelectedValue != "")
                StrSql.Value += " and FromUnitID='" + ddlDCDepartmentList.SelectedValue+"'";
            BindTransfer(0);
        }
    }

    protected void JumpList_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindTransfer(int.Parse(JumpList.SelectedValue));
    }
    protected void btnFirst_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = 0;
        BindTransfer((int)ViewState["PageIndex"]);
    }
    protected void btnPrev_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = (int)ViewState["PageIndex"] - 1;
        BindTransfer((int)ViewState["PageIndex"]);
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = (int)ViewState["PageIndex"] + 1;
        BindTransfer((int)ViewState["PageIndex"]);
    }
    protected void btnLast_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = (int)ViewState["PageCount"] - 1;
        BindTransfer((int)ViewState["PageIndex"]);
    }
    private void BindJumpList()
    {
        JumpList.Items.Clear();
        ListItem li;
        for (int i = 0; i < (int)ViewState["PageCount"]; i++)
        {
            li = new ListItem();
            li.Text = (i + 1).ToString();
            li.Value = i.ToString();
            JumpList.Items.Add(li);
        }
        JumpList.SelectedIndex = JumpList.Items.IndexOf(JumpList.Items.FindByValue(ViewState["PageIndex"].ToString()));
    }
    protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
    {
        SetPanel();
    }

    private void SetPanel()
    {
        this.Panel1.Visible = this.RadioButton1.Checked;
        this.Panel2.Visible = this.RadioButton2.Checked;
        this.Panel3.Visible = this.RadioButton3.Checked;

    }
}
